#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>
#include <string.h>
#include "support.h"
#include "head.h"
#include "find.h"
#include "message.h"
#include "clist.h"
#include "file.h"

struct findinfo_struct findinfo;

void
on_window_find_info_destroy            (GtkObject       *object,
                                        gpointer         user_data)
{
	gtk_widget_destroy(GTK_WIDGET(object));
}


void
on_button_find_info_help_clicked       (GtkButton       *button,
                                        gpointer         user_data)
{
	GtkWidget *window_find_info=(GtkWidget *)user_data;
	gchar msg[]="在查找的信息中，姓名不能为空!";
	message_box_for_find_info(window_find_info,msg);
}


void
on_button_find_info_cancel_clicked     (GtkButton       *button,
                                        gpointer         user_data)
{
	GtkWidget *window_find_info=(GtkWidget *)user_data;
	gtk_widget_destroy(window_find_info);
}


void
on_button_find_info_ok_clicked         (GtkButton       *button,
                                        gpointer         user_data)
{
	struct findinfo_struct *findinfo=(struct findinfo_struct *)user_data;
	struct clist_struct *clist=findinfo->clist;
	struct file_ctrl *global_file=findinfo->global_file;
	const gchar *info[2];
	gint i;
	gchar *pname,*pmail;
	
	info[0]=gtk_entry_get_text(GTK_ENTRY(findinfo->entry_name));
	if(strlen(info[0])==0) {
		on_button_find_info_help_clicked(NULL,findinfo->window);	
		return;
	}
	info[1]=gtk_entry_get_text(GTK_ENTRY(findinfo->entry_mail));
	if(strlen(info[1])==0) {
		for(i=0;i<global_file->cur_file->info_nums;i++) {
			gtk_clist_get_text(GTK_CLIST(clist->clist_show),i,0,&pname);
			if(strcmp(pname,info[0])==0) {
				gtk_clist_select_row(GTK_CLIST(clist->clist_show),i,0);
				gtk_widget_destroy(findinfo->window);
				return;
			}
		}
	} else {
		for(i=0;i<global_file->cur_file->info_nums;i++) {
			gtk_clist_get_text(GTK_CLIST(clist->clist_show),i,0,&pname);
			gtk_clist_get_text(GTK_CLIST(clist->clist_show),i,1,&pmail);
			if(strcmp(pname,info[0])==0&&strcmp(pmail,info[1])==0) {
				gtk_clist_select_row(GTK_CLIST(clist->clist_show),i,0);
				gtk_widget_destroy(findinfo->window);
				return;
			}
		}
	}
	gchar msg[]="在当前分组中没有找到该信息!";
	message_box_for_find_info(findinfo->window,msg);
	gtk_widget_destroy(findinfo->window);
}
